package org.example;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Bean对象的生命周期
 */
public class BeanLifecycle implements BeanNameAware, InitializingBean, DisposableBean {

    private String name;

    // 1. 实例化
    public BeanLifecycle() {
        System.out.println("构造函数调用");
    }

    // 2. 属性注入
    public void setName(String name) {
        this.name = name;
        System.out.println("属性注入");
    }

    // 3. Aware 接口
    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware: " + name);
    }

    // 4. @PostConstruct
    @PostConstruct
    public void postConstruct() {
        System.out.println("@PostConstruct 方法");
    }

    // 5. InitializingBean
    @Override
    public void afterPropertiesSet() {
        System.out.println("InitializingBean.afterPropertiesSet()");
    }

    // 6. 自定义初始化方法
    public void customInit() {
        System.out.println("自定义 init-method");
    }

    // 7. 使用阶段
    public void doWork() {
        System.out.println("Bean 使用中");
    }

    // 8. @PreDestroy
    @PreDestroy
    public void preDestroy() {
        System.out.println("@PreDestroy 方法");
    }

    // 9. DisposableBean
    @Override
    public void destroy() {
        System.out.println("DisposableBean.destroy()");
    }

    // 10. 自定义销毁方法
    public void customDestroy() {
        System.out.println("自定义 destroy-method");
    }

//    public void setBeanName(java.lang.String s) {
//
//    }
}
